﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using MS.InformationManagement.Presenter.ArchiveManagement;
using MS.InformationManagement.DAL.DocumentCenter;
using MS.InformationManagement.Support;
using MS.InformationManagement.Core.Support;
using MS.InformationManagement.Presenter.Support;

namespace MS.InformationManagement.WP.Layouts.MS.InformationManagement.WP
{
    public partial class ArchiveManagement : BaseApplicationPage, IArchiveManagementView
    {

        #region Fields

        ArchiveManagementPresenter _presenter;

        #endregion

        #region Properties

        public Archivo ArchiveEntity { get; set; }

        public int CurrentId
        {
            get { return int.Parse(Page.Request.QueryString["ID"]); }
        }

        public string LibraryTarget
        {
            get { return SPContext.Current.List.Title; }
        }

        #endregion

        #region Form Events


        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            //this.AddRibbonActions();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                _presenter = new ArchiveManagementPresenter(this);
                if (IsPostBack) return;
                _presenter.HandleLoad();
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void Action_Cancel(object sender, EventArgs e)
        {
            this.GoCurrentUserHome();
        }

        #endregion

        #region Implemented Methods

        public void Show()
        {
            lblUserName.Text = PeopleHelper.GetUserById(ArchiveEntity.Author.LookupId).Name;
            lnkFile.NavigateUrl = string.Format("javascript:editDocumentWithProgID2('{0}{1}', '', 'SharePoint.OpenDocuments', '0', '{0}', '0');",
                SPContext.Current.Web.Url, ArchiveEntity.DocumentUrl.Replace("/sites/docs", string.Empty));
            lblSolicitudDesc.Text = string.IsNullOrEmpty(ArchiveEntity.Solicitud) ? "Sin Solicitud en el Momento" : ArchiveEntity.Solicitud;
            lblResultado.Text = string.IsNullOrEmpty(ArchiveEntity.Respuesta) ? "Sin Observaciones" : ArchiveEntity.Respuesta;
            ctrLog.DocumentTraceEntries = ArchiveEntity.File.GetLog();
            string vbStatus = string.Empty;
            if (!ArchiveEntity.Aprobación.HasValue)
                vbStatus = "Sin Respuesta";
            else
            {
                if (ArchiveEntity.Aprobación.Value)
                    vbStatus = "Aprobado";
                else
                    vbStatus = "Rechazado";
            }
            lblResult.Text = vbStatus;
            lblFileName.Text = ArchiveEntity.Name;
        }

        #endregion

    }
}
